package cn.iocoder.yudao.module.eval.controller.admin.candidate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.candidate.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.candidate.EvalCandidateMaterialDO;
import cn.iocoder.yudao.module.eval.service.candidate.EvalCandidateMaterialService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 参评人员材料")
@RestController
@RequestMapping("/eval/candidate-material")
@Validated
public class EvalCandidateMaterialController {

    @Resource
    private EvalCandidateMaterialService candidateMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建参评人员材料")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:create')")
    public CommonResult<Long> createCandidateMaterial(@Valid @RequestBody EvalCandidateMaterialSaveReqVO createReqVO) {
        return success(candidateMaterialService.createCandidateMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新参评人员材料")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:update')")
    public CommonResult<Boolean> updateCandidateMaterial(@Valid @RequestBody EvalCandidateMaterialSaveReqVO updateReqVO) {
        candidateMaterialService.updateCandidateMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除参评人员材料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:delete')")
    public CommonResult<Boolean> deleteCandidateMaterial(@RequestParam("id") Long id) {
        candidateMaterialService.deleteCandidateMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得参评人员材料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:query')")
    public CommonResult<EvalCandidateMaterialRespVO> getCandidateMaterial(@RequestParam("id") Long id) {
        EvalCandidateMaterialDO candidateMaterial = candidateMaterialService.getCandidateMaterial(id);
        return success(BeanUtils.toBean(candidateMaterial, EvalCandidateMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得参评人员材料分页")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:query')")
    public CommonResult<PageResult<EvalCandidateMaterialRespVO>> getCandidateMaterialPage(@Valid EvalCandidateMaterialPageReqVO pageReqVO) {
        PageResult<EvalCandidateMaterialDO> pageResult = candidateMaterialService.getCandidateMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalCandidateMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出参评人员材料 Excel")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCandidateMaterialExcel(@Valid EvalCandidateMaterialPageReqVO exportReqVO,
                                             HttpServletResponse response) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalCandidateMaterialDO> list = candidateMaterialService.getCandidateMaterialList(exportReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "参评人员材料.xls", "数据", EvalCandidateMaterialRespVO.class,
                BeanUtils.toBean(list, EvalCandidateMaterialRespVO.class));
    }

    @GetMapping("/list-by-candidate")
    @Operation(summary = "根据参评人员ID获取材料列表")
    @Parameter(name = "candidateInfoId", description = "参评人员ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:query')")
    public CommonResult<List<EvalCandidateMaterialRespVO>> getCandidateMaterialListByCandidateId(@RequestParam("candidateInfoId") Long candidateInfoId) {
        List<EvalCandidateMaterialDO> list = candidateMaterialService.getCandidateMaterialListByCandidateId(candidateInfoId);
        return success(BeanUtils.toBean(list, EvalCandidateMaterialRespVO.class));
    }

    @PutMapping("/audit")
    @Operation(summary = "审核参评人员材料")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:audit')")
    public CommonResult<Boolean> auditCandidateMaterial(@Valid @RequestBody EvalCandidateMaterialAuditReqVO auditReqVO) {
        candidateMaterialService.auditCandidateMaterial(auditReqVO.getId(), auditReqVO.getAuditStatus(), auditReqVO.getAuditComments());
        return success(true);
    }

    @PostMapping("/batch-audit")
    @Operation(summary = "批量审核参评人员材料")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:audit')")
    public CommonResult<Boolean> batchAuditCandidateMaterial(@Valid @RequestBody EvalCandidateMaterialBatchAuditReqVO batchAuditReqVO) {
        candidateMaterialService.batchAuditCandidateMaterial(batchAuditReqVO.getIds(), batchAuditReqVO.getAuditStatus(), batchAuditReqVO.getAuditComments());
        return success(true);
    }

    @GetMapping("/check-completeness")
    @Operation(summary = "检查材料完整性")
    @Parameter(name = "candidateInfoId", description = "参评人员ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:candidate-material:query')")
    public CommonResult<Object> checkMaterialCompleteness(@RequestParam("candidateInfoId") Long candidateInfoId) {
        return success(candidateMaterialService.checkMaterialCompleteness(candidateInfoId));
    }

}
